var path = require('path')
var express = require('express')
// var compression = require('compression')
// var morgan = require('morgan')

// default port where dev server listens for incoming traffic
var port = process.env.PORT || 8089
console.log('$env: ', process.env.NODE_ENV)
var env = process.env.NODE_ENV || 'dev'
var server = express()

// logfile
// server.use(morgan('tiny'))

//  前端加healthCheck
server.use('/healthCheck', function (req, res) {
  res.json({
    status: 200,
    statusText: 'healthy'
  })
})

// handle fallback for HTML5 history API
server.use(require('connect-history-api-fallback')())

// compression files js,image,css
// server.use(compression())

// js css目录, 缓存三天
server.use('/static', express.static(path.join(__dirname, 'dist/static'), {
  maxAge: '3d'
}))

// 根目录不缓存, 防止上线白屏
server.use(express.static(path.join(__dirname, 'dist'), {
  // maxAge: '3d'
}))

module.exports = server.listen(port, function (err) {
  if (err) {
    console.log(err)
    return
  }
  var uri = 'http://localhost:' + port
  console.log('Listening at ' + uri + '\n')
})
